import { defineStore } from 'pinia'
import { post } from '@/api/http';
import { Message } from '@arco-design/web-vue'

export const useFinishedProductInstockStore = defineStore('finished-product-instock', {
  state: () => {
    return {
      warehouseId: '',
      shelveCodeList: [],
      kwList: [],
    }
  },
  actions: {
    async getShelveCodeList(warehouseId) {
      try {
        this.shelveCodeList = []
        this.kwList = []
        const res = await post({
          url: 'wms/shelve/getFieldValueOption',
          data: {
            shelveCode: '',
            warehouseId,
          },
        })
        if (res.code == 200) {
          this.shelveCodeList = res.data.map((item: any) => {
            return {
              label: item.shelveCode,
              value: item.shelveCode
            }
          })
        } else {
          throw new Error(res.message);
        }
      } catch (e) {
        console.log(e)
      }
    },
    async getKwList(storageName) {
      if (this.warehouseId == '') {
        Message.error({
          content: "请先选择上方仓库名称！",
          duration: 5000,
        });
      } else {
        try {
          const res = await post({
            url: "/wms/Storage/getFieldValue",
            data: {
              warehouseId: this.warehouseId,
              storageName
            }
          })
          const { data, code } = res
          if (code === 200) {
            this.kwList = data.map((item: any) => {
              return {
                label: item.storageName,
                value: item.storageName

              }
            });
          } else {
            throw new Error(res.message);
          }
        } catch (e) {
          console.log(e)
        }
      }

    }
  }
})